<template>

    <a-modal v-model:visible="LAYER.visible" :title="LAYER.title" :width="LAYER.width"
             :align-center="LAYER.alignCenter" :top="LAYER.top"
             :closable="LAYER.closable" :mask-closable="LAYER.maskClosable" :esc-to-close="LAYER.escToClose"
             :unmount-on-close="LAYER.unmountOnClose" :footer="LAYER.footer" :ok-text="LAYER.okText"
             :ok-loading="LAYER.okLoading" :cancel-text="LAYER.cancelText" :on-before-ok="onSubmit" @close="onCancel">

      <a-form ref="formRef" :model="formState">
        <a-form-item field="code" required label="分配">
          <a-select v-model="formState.code" :options="accountList" allow-search></a-select>
        </a-form-item>
      </a-form>
    </a-modal>

</template>
<script lang="ts" setup>

import {reactive, ref} from "vue";
import {EditModal} from "@/libs/core";
import {TASK_MAIN_DETAIL_EDIT} from '../resource/form'
import {CustomerHttp} from '../resource/http'
import {EmptyUtil, RequestUtil} from "@/utils";
import {SystemOptions, WorkOptions} from "@/libs/options";


const props = defineProps({record: {type: Object, required: true}})
const emit = defineEmits(['cancel'])
const formData = reactive({
  id: null,
  code: null,
  isRead: true,
});

const {BODY, RULES} = TASK_MAIN_DETAIL_EDIT
const config = {tplRecord: BODY, rawRecord: props.record, rowKey: 'id', emit}
// 将备注委托到封装类的执行方法上
const service = new EditModal(config).setAddRequest(CustomerHttp.addModificationMain);

const {formRef, formState, LAYER, onCancel, onSubmit} = service
service.isUpdate = false
service.LAYER.okText = "确定"
const isUpdate = EmptyUtil.isEmptyObject(props.record.account);
const title = isUpdate ? '分配客服' : '工单下发'
service.setMD(title, 500)
// eslint-disable-next-line vue/no-setup-props-destructure
formData.id = props.record.id

const accountList = ref([]);
const isCustomerService = async () => {
  formData.id = props.record.id;
  // 判断是分配客服还是分配管家
  if (EmptyUtil.isEmptyObject(props.record.account)) {
    // 提交 分配客服
    const {data} = await RequestUtil.getAjax(SystemOptions.CustomerServiceOptions)
    accountList.value = data;
    formData.isRead = true
  } else {
    formData.isRead = false
    // 提交 分配管家
    if (EmptyUtil.isEmptyObject(props.record.staff)) {
      const {data} = await RequestUtil.getAjax(WorkOptions.StaffIdOptions)
      accountList.value = data;
    }
  }

}
isCustomerService();


</script>

